﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using Showroom.BLL;

namespace ShowroomMobile
{
    public partial class ProductDetails : System.Web.UI.Page
    {
        private int ProductID
        {
            get
            {
                return string.IsNullOrEmpty(Request["id"]) ? 0 : Convert.ToInt32(Request["id"]);
            }
        }

        private int CategoryID
        {
            get
            {
                return string.IsNullOrEmpty(Request["cid"]) ? 0 : Convert.ToInt32(Request["cid"]);
            }
        }

        protected string FacebookCommentLink
        {
            get
            {
                return Request.Url.ToString();
            }
        }

        protected int TotalPicture
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(Request["isreview"]))
                {
                    View_ListProducts cProds;
                    string cacheKey = "ListProducts_" + CategoryID.ToString();
                    if (Cache[cacheKey] == null)
                    {
                        cProds = View_ListProducts.GetListByCategoryID(CategoryID);
                        //Cache
                        Cache.Add(cacheKey, cProds, null, System.DateTime.Now.AddHours(Convert.ToInt32(Utilities.GetKeySetting("CacheTimeOut"))), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
                    }
                    else
                    {
                        cProds = (View_ListProducts)Cache[cacheKey];
                    }
                    cProds.Filter = View_ListProducts.ColumnNames.ProductID + "=" + ProductID.ToString();
                    if (cProds.RowCount == 0)
                    {
                        Response.Redirect("~/404.aspx"); return;
                    }
                    lblProductName.Text = cProds.s_Name;
                    lblProductDetail.Text = cProds.s_Description;
                    lblPrice.Text = (string.IsNullOrEmpty(cProds.s_Price) || cProds.Price == 0) ? "" : String.Format("{0:C}", cProds.Price);
                    Page.Title = cProds.s_Name;

                    pictureLarge.InnerHtml = string.Format("<span></span><img src='{0}' alt='{1}' />", Utilities.GetKeySetting("PathUploadImageOriginal") + "/" + cProds.ImageUrl, cProds.s_Name);

                    //Load image
                    ProductImages pImgs = ProductImages.GetByProductID(cProds.ProductID);
                    StringBuilder strImgs = new StringBuilder();
                    TotalPicture = pImgs.RowCount;
                    for (int i = 0; i < pImgs.RowCount; i++)
                    {
                        strImgs.Append(string.Format("<li {0}><a href='#' title='{1}' mainpicture='{2}' rel='product_picture'><span></span><img src='{3}' alt='{1}' /></a></li>"
                            , (pImgs.Default) ? "class='current'" : "", cProds.s_Name, Utilities.GetKeySetting("PathUploadImageOriginal") + "/" + pImgs.s_OriginalUrl,
                            Utilities.GetKeySetting("PathUploadImageThumb") + "/" + pImgs.s_OriginalUrl
                            ));
                        pImgs.MoveNext();
                    }
                    blockThumbs.InnerHtml = strImgs.ToString();

                    //Get next product
                    cProds.Filter = View_ListProducts.ColumnNames.ProductID + ">" + ProductID.ToString();
                    cProds.Sort = View_ListProducts.ColumnNames.ProductID + " ASC";
                    if (cProds.RowCount > 0)
                    {
                        ucNextPrevHead.NextUrl = Request.Url.ToString().Replace("&id=" + ProductID.ToString(), "&id=" + cProds.s_ProductID);
                    }

                    //Get next product
                    cProds.Filter = View_ListProducts.ColumnNames.ProductID + "<" + ProductID.ToString();
                    cProds.Sort = View_ListProducts.ColumnNames.ProductID + " DESC";
                    if (cProds.RowCount > 0)
                    {
                        ucNextPrevHead.PrevUrl = Request.Url.ToString().Replace("&id=" + ProductID.ToString(), "&id=" + cProds.s_ProductID);
                    }
                    cProds.Filter = string.Empty;
                    cProds.Rewind();
                }
                else
                {
                    View_Products prod = View_Products.GetByProductID(ProductID);
                    if (prod.RowCount < 0)
                    {
                        Response.Redirect("~/404.aspx"); return;
                    }
                    lblProductName.Text = prod.s_Name;
                    lblProductDetail.Text = prod.s_Description;
                    lblPrice.Text = (string.IsNullOrEmpty(prod.s_Price) || prod.Price == 0) ? "" : String.Format("{0:C}", prod.Price);
                    Page.Title = prod.s_Name;

                    pictureLarge.InnerHtml = string.Format("<span></span><img src='{0}' alt='{1}' />", Utilities.GetKeySetting("PathUploadImageOriginal") + "/" + prod.ImageUrl, prod.s_Name);

                    //Load image
                    ProductImages pImgs = ProductImages.GetByProductID(prod.ID);
                    StringBuilder strImgs = new StringBuilder();
                    TotalPicture = pImgs.RowCount;
                    for (int i = 0; i < pImgs.RowCount; i++)
                    {
                        strImgs.Append(string.Format("<li {0}><a href='#' title='{1}' mainpicture='{2}' rel='product_picture'><span></span><img src='{3}' alt='{1}' /></a></li>"
                            , (pImgs.Default) ? "class='current'" : "", prod.s_Name, Utilities.GetKeySetting("PathUploadImageOriginal") + "/" + pImgs.s_OriginalUrl,
                            Utilities.GetKeySetting("PathUploadImageThumb") + "/" + pImgs.s_OriginalUrl
                            ));
                        pImgs.MoveNext();
                    }
                    blockThumbs.InnerHtml = strImgs.ToString();
                }
            }
        }
    }
}